package com.travel.find.findadapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.travel.R;
import com.travel.find.findactivity.FindRVActivity;
import com.travel.find.findactivity.FindVPActivity;
import com.travel.find.findbean.FindRVBean;
import com.travel.find.findbean.FindTablayoutBean;
import com.travel.find.findinterface.IFindRecyclerViewItemClick;
import com.travel.find.findutils.BannerLayout;
import com.travel.find.findutils.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/18.
 */
public class FindRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private static final int TYPE_ITEM = 0;  //普通Item View
    private static final int TYPE_FOOTER = 1;  //顶部FootView
    public static final int TYPE_LAST = 2;//lunbo
    //上拉加载更多状态-默认为0
    public static int load_more_status = 0;
    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //recyclerview 集合
    private List<FindRVBean.DataBean.ListBean> list;
    //viewpager集合
    private List<FindTablayoutBean.DataBean.IndexAdListBean> vpList= new ArrayList<>();
    private BannerLayout bannerLayout;


    private IFindRecyclerViewItemClick mFindRecyclerViewItemClick;

    public FindRecyclerViewAdapter(Context context, List<FindRVBean.DataBean.ListBean> list, List<FindTablayoutBean.DataBean.IndexAdListBean> vpList) {
        this.context = context;
        this.list = list;
        this.vpList = vpList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //进行判断显示类型，来创建返回不同的View
        if (viewType == TYPE_ITEM) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.newest_recyclerview_item, parent, false));
            return holder;
        } else if (viewType == TYPE_FOOTER) {
            View foot_view = LayoutInflater.from(context).inflate(R.layout.recycler_load_more_layout, parent, false);
            //这边可以做一些属性设置，甚至事件监听绑定
//            foot_view.setBackgroundColor(Color.RED);
            FootViewHolder footViewHolder = new FootViewHolder(foot_view);
            return footViewHolder;
        }else if(viewType == TYPE_LAST){
            FirstViewHolder firstViewHolder = new FirstViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.find_newest_viewpager, parent, false));
            return  firstViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        //点击item跳转到findrecyclerview webview
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = list.get(position).getUrl();
                String title = list.get(position).getTitle();
                Intent intent = new Intent(context, FindRVActivity.class);
                intent.putExtra("url", url);
                intent.putExtra("title", title);
                context.startActivity(intent);
            }
        });
        //item的布局控件
        if (holder instanceof MyViewHolder) {
            String poiName = null;
            String cityName = null;
            String countryName = null;
            if (list.get(position - 1).getTitle() != null) {
                ((MyViewHolder) holder).tv_find_newest_title.setText(list.get(position - 1).getTitle());
            }
            if (list.get(position - 1).getAuthor() != null) {
                ((MyViewHolder) holder).tv_find_newest_author.setText(list.get(position - 1).getAuthor());
            } else {
                ((MyViewHolder) holder).tv_find_newest_author.setText("小词词");
            }

            ((MyViewHolder) holder).tv_newest_item_views.setText(list.get(position - 1).getViews() + "");
            if (list.get(position - 1).getCountryName() == null || "".equals(list.get(position - 1).getCountryName())) {
                countryName = "";
            } else {
                countryName = list.get(position - 1).getCountryName();
            }
            if (list.get(position - 1).getCityName() == null || "".equals(list.get(position - 1).getCityName())) {
                cityName = "";
            } else {
                cityName = " - " + list.get(position - 1).getCityName();
            }
            if (list.get(position - 1).getPoiName() == null || "".equals(list.get(position - 1).getPoiName())) {
                poiName = "";
            } else {
                poiName = " - " + list.get(position - 1).getPoiName();
            }

            ((MyViewHolder) holder).tv_newest_item_place.setText(countryName + cityName + poiName);
            if (list.get(position - 1).getPic() == null || "".equals(list.get(position - 1).getPic())) {
                ((MyViewHolder) holder).iv_find_newest_image.setVisibility(View.GONE);
            } else {
//                Log.e("tag","!!!!!!!!!!"+list.get(position).getPic());
                ((MyViewHolder) holder).iv_find_newest_image.setVisibility(View.VISIBLE);
                Glide.with(context).load(list.get(position - 1).getPic())
                        .centerCrop()
                        .placeholder(R.mipmap.default_square_large)
                        .error(R.mipmap.default_faile_image)
                        .into(((MyViewHolder) holder).iv_find_newest_image);
            }
            if (list.get(position - 1).getMemberInfo().getAvatar() != null || !"".equals(list.get(position - 1).getMemberInfo().getAvatar())) {
                Glide.with(context).load(list.get(position - 1).getMemberInfo().getAvatar())
                        .centerCrop()
                        .placeholder(R.mipmap.empty_user_circle)
                        .transform(new GlideCircleTransform(context))
                        .into(((MyViewHolder) holder).iv_newest_item_avatar);
            }
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
//            Log.e("tag","%%%%%%%%%%%%%%%%%%%"+list.size());
            switch (load_more_status) {
                case PULLUP_LOAD_MORE:
                    footViewHolder.tv_jiazai.setText("正在加载更多...");
                    Glide.with(context).load(R.mipmap.jiazai).into(footViewHolder.iv_jiazai);
                    break;
                case LOADING_MORE:
                    footViewHolder.tv_jiazai.setText("已经没有更多了...");
                    Glide.with(context).load(R.mipmap.jiazai).into(footViewHolder.iv_jiazai);
                    break;
            }
        } else if (holder instanceof FirstViewHolder) {
            FirstViewHolder firstViewHolder = (FirstViewHolder) holder;
            List<String> mList = new ArrayList<>();
            if (vpList.size() != 0 || vpList != null) {
                for (int i = 0; i < vpList.size(); i++) {
                    mList.add(vpList.get(i).getPic());
                }
            }
            bannerLayout.setViewUrls(mList);
            bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    String title = vpList.get(position).getTitle();
                    String url = vpList.get(position).getUrl();
                    Intent intent = new Intent(context, FindVPActivity.class);
                    intent.putExtra("title", title);
                    intent.putExtra("url", url);
                    context.startActivity(intent);
                }
            });
        }
    }

    //注意事项：list.size()+1
    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    //注意事项
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_LAST;
        } else if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    //item布局控件
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_find_newest_title, tv_find_newest_author, tv_find_newest_zan, tv_newest_item_views, tv_newest_item_place;
        ImageView iv_find_newest_image, iv_newest_item_avatar;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_find_newest_image = (ImageView) itemView.findViewById(R.id.iv_newest_item_image);
            iv_newest_item_avatar = (ImageView) itemView.findViewById(R.id.iv_newest_item_avatar);
            tv_find_newest_title = (TextView) itemView.findViewById(R.id.tv_newest_item_text);
            tv_find_newest_author = (TextView) itemView.findViewById(R.id.tv_newest_item_autor);
            tv_find_newest_zan = (TextView) itemView.findViewById(R.id.tv_newsest_item_zan);
            tv_newest_item_views = (TextView) itemView.findViewById(R.id.tv_newest_item_views);
            tv_newest_item_place = (TextView) itemView.findViewById(R.id.tv_newest_item_place);
        }
    }

    //上拉加载布局控件
    class FootViewHolder extends RecyclerView.ViewHolder {
        TextView tv_jiazai;
        ImageView iv_jiazai;

        public FootViewHolder(View itemView) {
            super(itemView);
            tv_jiazai = (TextView) itemView.findViewById(R.id.tv_jiazai);
            iv_jiazai = (ImageView) itemView.findViewById(R.id.iv_jiazai);
        }
    }

    //viewpager轮播holder
    class FirstViewHolder extends RecyclerView.ViewHolder {
        public FirstViewHolder(View itemView) {
            super(itemView);
            bannerLayout = (BannerLayout) itemView.findViewById(R.id.find_vp);
        }
    }

    //分页添加到list集合
    public void rush(List<FindRVBean.DataBean.ListBean> mlist, boolean isClear) {
        if (isClear) {
            list.clear();
        }
        list.addAll(mlist);
        notifyDataSetChanged();
    }
}

